package com.cv4j.app.fragment;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.datamodel.ByteProcessor;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ColorProcessor;
import com.cv4j.core.hist.GaussianBackProjection;

/**
 * 直方图反向投影-高斯反向投影界面
 *
 * @since 2021-05-18
 */
public class GaussianBackFragment {
    private Context mContext;
    private Component component;
    private Image image0;
    private Image image1;
    private Image result;

    private GaussianBackFragment(Context context) {
        mContext = context;
        init();
    }

    /**
     * getInstance
     *
     * @param context context
     * @return GaussianBackFragment
     */
    public static GaussianBackFragment getInstance(Context context) {
        GaussianBackFragment compareHist1Fragment = new GaussianBackFragment(context);
        return compareHist1Fragment;
    }

    private void init() {
        component = LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_fragment_back_project, null, false);
        component.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_PARENT));
        image0 = (Image) component.findComponentById(ResourceTable.Id_image0);
        image1 = (Image) component.findComponentById(ResourceTable.Id_image1);
        result = (Image) component.findComponentById(ResourceTable.Id_result);
        initData();
    }

    private void initData() {
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        PixelMap pixelMap1 = PixelMapUtil.getPixelMapFromResource(
            mContext, ResourceTable.Media_test_project_target).get();
        Size size1 = pixelMap1.getImageInfo().size;
        options.size = new Size((int) (size1.width * 1.5), (int) (size1.height * 1.5));
        PixelMap newPixelMap1 = PixelMap.create(pixelMap1, options);
        image0.setPixelMap(newPixelMap1);
        PixelMap pixelMap2 = PixelMapUtil.getPixelMapFromResource(
            mContext, ResourceTable.Media_test_project_sample).get();
        Size size2 = pixelMap2.getImageInfo().size;
        options.size = new Size((int) (size2.width * 1.5), (int) (size2.height * 1.5));
        PixelMap newPixelMap2 = PixelMap.create(pixelMap2, options);
        image1.setPixelMap(newPixelMap2);

        CV4JImage cv4jImage = new CV4JImage(newPixelMap1);
        ColorProcessor colorProcessor = (ColorProcessor) cv4jImage.getProcessor();
        int width = colorProcessor.getWidth();
        int height = colorProcessor.getHeight();

        // 反向投影结果
        CV4JImage resultCV4JImage = new CV4JImage(width, height);
        ByteProcessor byteProcessor = (ByteProcessor) resultCV4JImage.getProcessor();

        // sample
        CV4JImage sample = new CV4JImage(newPixelMap2);
        ColorProcessor sampleProcessor = (ColorProcessor) sample.getProcessor();
        GaussianBackProjection gaussianBackProjection = new GaussianBackProjection();
        gaussianBackProjection.backProjection(colorProcessor, sampleProcessor, byteProcessor);
        result.setPixelMap(byteProcessor.getImage().toBitmap());
    }

    public Component getComponent() {
        return component;
    }
}
